﻿using Bilibili.Model.Resource;
using Bilibili.Model.Wallet;
using PCLUntils.Json;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class WalletClient : ClientBase
    {
        public async Task<ReturnModel<WalletModel>> GetBCoin()
        {
            try
            {
                string results = await ApiClient.GetResults(string.Format("https://pay.bilibili.com/user/info/v2"));
                var model = results.ParseObject<WalletModel>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<WalletModel>(ex);
            }
        }
        public async Task<ReturnModel<CoinInfoModel>> GetCoin()
        {
            try
            {
                var results = await ApiClient.GetResults(string.Format("https://account.bilibili.com/home/userInfo"));
                var model = results.ParseObject<CoinInfoModel>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<CoinInfoModel>(ex);
            }
        }
        public async Task<ReturnModel<CoinsModel>> GetMoneyLog()
        {
            try
            {
                string uri = string.Format("https://account.bilibili.com/log/getMoneyLog?page=1");
                string results = await ApiClient.GetResults(uri);
                var model = results.ParseObject<CoinsModel>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<CoinsModel>(ex);
            }
        }
        public async Task<ReturnModel<PayOrdersModel>> PayOrders(int pageNum)
        {
            try
            {
                string uri = string.Format("https://pay.bilibili.com/bp/payOrders?pageSize=20&pageNo={0}&beginTime=2015-01-1&endTime={1}", pageNum, DateTime.Now.ToString("yyyy-MM-dd"));
                string results = await ApiClient.GetResults(uri);
                var model = results.ParseObject<PayOrdersModel>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<PayOrdersModel>(ex);
            }
        }
        public async Task<ReturnModel<PayQuanModel>> GetCouponList(int pageNum)
        {
            try
            {
                string uri = string.Format("https://pay.bilibili.com/coupon/list.do?pageSize=20&pageNo={0}&beginTime=2015-01-1&endTime={1}",
                    pageNum, DateTime.Now.ToString("yyyy-MM-dd"));
                string results = await ApiClient.GetResults(uri);
                var model = results.ParseObject<PayQuanModel>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<PayQuanModel>(ex);
            }
        }
        public async Task<ReturnModel<PayOrdersModel>> RechargeOrders(int pageNum)
        {
            try
            {
                string uri = string.Format("https://pay.bilibili.com/bp/rechargeOrders?pageSize=20&pageNo={0}&beginTime=2015-01-1&endTime={1}",
                    pageNum, DateTime.Now.ToString("yyyy-MM-dd"));
                string results = await ApiClient.GetResults(uri);
                PayOrdersModel model = results.ParseObject<PayOrdersModel>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<PayOrdersModel>(ex);
            }
        }
    }
}